package com.nxn.core.web.action.test;
/*
 * Copyright (c) scott.cgi Rights Reserved.
 * Email: scott.cgi@gmail.com
 
package com.lrs.s2sh.web.action.test;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import com.lrs.s2sh.web.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

*//**
 * 提供CRUD操作的Action
 * 
 * @author scott.Cgi
 * @since  2010-8-25 
 *//*
public abstract class CRUDAction<T, PK extends Serializable> extends BaseAction
		implements ModelDriven<T>, Preparable {

	
	private static final long	serialVersionUID	= 7909985827753544081L;

	@Resource(name = "crudService")
	private CrudService<T, PK> crudService;
	
	// 实体对象列表
	protected List<T> entityList = Collections.<T>emptyList();
	// 实体对象
	protected T entity;
	// 实体类主键
	protected PK id;
	// 实体类的名字
	protected String entityClassName = "";
	
	// 默认分页排序,子类选择性覆盖
	protected String orderBy = "id desc";
	// 分页对象
	protected Page<T> page;
	
	// 本页第一条数据在总数据中的位置
	private String start;
	// 分页大小
	private String limit;
	// 批量删除的id
	private String[] ids;

	
	
	
	*//**
	 * 根据泛型类型实例化对象
	 * 
	 * @return 泛型类型的对象
	 * @throws Exception
	 *//*
	@SuppressWarnings("unchecked")
	public T instanceAnnotationObject() throws Exception {
		Class<?> cl = this.getClass();
		// 得到泛型类型参数数组就是<>里面的2个值
		Type[] types = ((ParameterizedType) cl.getGenericSuperclass()).getActualTypeArguments();
		try {
			return ((Class<T>) types[0]).newInstance();
		} catch (Exception e) {
			log.error("crudAction的泛型类型实例化失败! 错误信息: {}", e.getMessage());
			throw e;
		} 
	}
	
	 (non-Javadoc)
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 
	@SuppressWarnings("unchecked")
	@Override
	public void prepare() throws Exception {
		// 利用反射新建enity对象
		this.entity = this.instanceAnnotationObject();
		this.entityClassName = this.entity.getClass().getSimpleName();
		
		String id = this.request.getParameter("id");
		if(id != null) {
			// 根据主键从数据库查找entity
			try {
				this.id = (PK) Long.valueOf(id);
				T dataEntity = crudService.read((Class<T>)entity.getClass(), this.id);
				if(dataEntity != null) {
					this.entity = dataEntity;
				} else {
					this.entity = this.instanceAnnotationObject();
				}
			} catch(NumberFormatException e) {
				log.error("实体对象对象[{}]的id:{}转换数字失败!", this.entityClassName, id);
			} catch (Exception e) {
				log.error("读取实体对象[{}]失败! id: {}, 错误原因 : {}",new Object[]{
						this.entityClassName,
						id,
						e.getMessage()
				});
				
				throw e;
			}
		}
	}

	 (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 
	@Override
	public T getModel() {
		return entity;
	}
	
	
	*//**
	 * 读取实体类对象
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doRead() {
		// prepare已经完成此功能
		// 使用getModel()获得实体类
		log.info("成功读取实体对象[{}]", this.entityClassName);
		return true;
	}
	
	
	*//**
	 * 获得实体类分页列表
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doListPage() {
		try {
			int pageSize = Integer.valueOf(limit);
			// 计算当前页面号
			int pageNo = Integer.valueOf(start) / pageSize + 1; 

			this.page = crudService.readPage(entity.getClass(), pageNo, pageSize, this.orderBy);
		} catch (ServiceException e) {
			log.error("读取实体类[{}]列表失败! 错误原因 : {}", new Object[]{
					entity.getClass().getName(),
					e.getMessage()
			});
			
			return false;
		}
		
		log.info("成功读取实体对象[{}]列表", this.entityClassName);
		return true;
	}
	
	*//**
	 * 获得实体类所有对象列表
	 * 
	 * @return 操作是否成功
	 *//*
	@SuppressWarnings("unchecked")
    public boolean doList() {
		try{
			this.entityList = crudService.readList((Class<T>)entity.getClass());
		} catch(ServiceException e){
			log.error("读取实体类[{}]列表失败! 错误原因: {}", this.entityClassName, e.getMessage());
			return false;
		}
		
		return true;
	}
	
	*//**
	 * 创建实体类数据
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doCreate() {
		try {
			crudService.createOrUpdate(entity);
		} catch (ServiceException e) {
			log.error("创建实体对象[{}]失败! 错误原因  : {}", this.entityClassName, e.getMessage());
			
			return false;
		}
		
		log.info("成功创建实体对象[{}]", this.entityClassName);
		return true;
	}
	
	*//**
	 * 更新实体类数据
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doUpdate() {
		try {
			crudService.createOrUpdate(entity);
		} catch (Exception e) {
			log.error("更新实体对象[{}]失败! 错误原因  : {}", this.entityClassName, e.getMessage());
			
			return false;
		}
		
		log.info("成功更新实体对象[{}]", this.entityClassName);
		return true;
	}
	
	*//**
	 * 删除实体对象数据
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doDelete() {
		try {
			crudService.delete(entity);
		} catch (Exception e) {
			log.error("删除实体对象[{}]失败! 错误原因  : {}", this.entityClassName, e.getMessage());
			
			return false;
		}
		
		log.info("成功删除实体对象[{}]", this.entityClassName);
		return true;
	}
	
	*//**
	 * 批量删除
	 * 
	 * @return 操作是否成功
	 *//*
	public boolean doBatchDelete() {
		try {
			StringBuilder sb = new StringBuilder();
			
			for(String id : ids) {
				sb.append(id).append(",");
			}
			
			if(sb.lastIndexOf(",") == sb.length() - 1) {
				sb.deleteCharAt(sb.length() - 1);
			}
			
			log.info("批量删除的实体类[{}]的ids: {}", this.entityClassName, sb.toString());
			
			crudService.batchDelete(this.entityClassName, sb.toString());
		} catch(Exception e) {
			log.error("批量删除实体对象[{}]失败! 错误原因: {}", this.entityClassName, e.getMessage());
			
			return false;
		}
		
		
		return true;
	}

	*//**
	 * @return the entity
	 *//*
	public T getEntity() {
		return entity;
	}

	*//**
	 * @param entity the entity to set
	 *//*
	public void setEntity(T entity) {
		this.entity = entity;
	}


	*//**
	 * @return the id
	 *//*
	public PK getId() {
		return id;
	}


	*//**
	 * @param id the id to set
	 *//*
	public void setId(PK id) {
		this.id = id;
	}

	*//**
	 * @return the page
	 *//*
	public Page<T> getPage() {
		return page;
	}

	*//**
	 * @param page the page to set
	 *//*
	public void setPage(Page<T> page) {
		this.page = page;
	}

	*//**
	 * @return the start
	 *//*
	public String getStart() {
		return start;
	}

	*//**
	 * @param start the start to set
	 *//*
	public void setStart(String start) {
		this.start = start;
	}

	*//**
	 * @return the limit
	 *//*
	public String getLimit() {
		return limit;
	}

	*//**
	 * @param limit the limit to set
	 *//*
	public void setLimit(String limit) {
		this.limit = limit;
	}

	*//**
     * @return the ids
     *//*
    public String[] getIds() {
    	return ids;
    }

	*//**
     * @param ids the ids to set
     *//*
    public void setIds(String[] ids) {
    	this.ids = ids;
    }
}
*/